// pages/mine/blacklist/index.js
const app = getApp();
const utils = require("../../../utils/util");
const config = require("../../../utils/config");
import { http } from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blacklist:[],
    pageIndex: 1,
    isload: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.queryBlacklist();
  },

  /** 黑名单列表 */
  queryBlacklist(){
    let that = this;
    http.requestData("GET","MemberBlack/GetListPage",{
      // openid: config.OPENID()
      pageIndex: this.data.pageIndex,
      pageSize: 10
    }).then(res=>{
      if(res && res.Tag == 10){
        if(that.data.pageIndex == 1){
          that.data.blacklist = []
        }
        res.Data.forEach(item=>{
          item.CreateTime = utils.formatTime(new Date(item.CreateTime));
          that.data.blacklist.push(item);
        })
        that.setData({
          blacklist: that.data.blacklist,
          isload: res.Data.length<10?true:false
        })
      }else{
        http.showToast(res.Message)
      }
    }).catch(e=>{
      http.showToast("网络服务异常")
    })
  },

  /** 移除 */
  delTap(e){
    let that = this;
    let i = e.currentTarget.dataset.i;
    wx.showModal({
      title: '提示',
      content: '确定将用户移除黑名单吗？',
      complete: (res) => {
        if (res.cancel) {
        }
        if (res.confirm) {
          that.removeUser(i);
        }
      }
    })
  },
  removeUser(current){
    let that = this;
    let person = this.data.blacklist[current];
    http.requestData("POST","MemberBlack/Remove",{
      InviteCode: person.InviteCode,
      MemberId: person.MemberId
    }).then(res=>{
      if(res.Tag == 10){
        that.data.blacklist.splice(current,1);
        that.setData({
          blacklist: that.data.blacklist
        })
      }else{
        http.showToast(res.Message)
      }
    }).catch(e=>{
      http.showToast("移除失败")
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isload){
      return
    }
    this.setData({
      pageIndex: this.data.pageIndex+1
    })
    this.queryBlacklist();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})